package com.sooncode.project.focusservice.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.sooncode.project.core.annotations.Lookup;
import com.sooncode.project.core.annotations.LookupModel;
import com.sooncode.project.core.generic.BasicModifyEvent;
import com.sooncode.project.core.model.DomainException;
import com.sooncode.project.focusservice._.base.BaseDomainModel;
import com.sooncode.project.focusservice._.request.OrgSyncClient;
import com.sooncode.project.focusservice._.request.UserClient;
import com.sooncode.project.focusservice.model.VO.Password;
import com.sooncode.project.springcloudcommon.utils.DateUtils;
import lombok.Data;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Data
@LookupModel
public class Organization extends BaseDomainModel<Organization> {
    private String typeId;
    @Lookup(fromModel = OrganizationType.class,localField = "typeId",fromField = "name")
    private String typeName;
    private String usci;
    private String usciImage;
    private String name;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date inDate;
    private String address;
    private String city;
    private String gps;
    private String manager;
    private String managerPhone;
    private String managerCard;
    private String accountId;
    private int deviceCount;
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    private Date expireDate;
    private String status;
    private Map<String,String> deviceMac;
    private Password devicePassword;
    private int memberSerial;
    private String syncOrgId;
    private String c_name;
    private String c_printFoot;
    private int testNumber=0;
    private int courseNumber=0;
    private String serviceType="time";//frequency:按次数, time:按时间
    private String[] apps;
    public void add(Map map, UserClient client, OrgSyncClient orgSyncClient){
        if(!map.containsKey("managerPhone")||map.get("managerPhone").toString().equals(""))
            throw new DomainException("手机号不能为空");
        String managerPhone=map.get("managerPhone").toString();
        map.put("inData",new Date());
        if(client.checkUserExist(managerPhone))
            throw new DomainException("账号已存在");
        String accountId=client.addNewUser(managerPhone,map.get("manager").toString(),"focus");
        String phont=map.get("managerPhone").toString();
        map.put("devicePassword",new Password(Password.encrypt(phont.substring(phont.length()-6))));
        map.put("accountId",accountId);
        try{
            Map<String,Object> orgMap=new HashMap();
            orgMap.put("name",map.get("name"));
            orgMap.put("inDate", DateUtils.formatDate(new Date()));
            orgMap.put("address",map.get("address"));
            orgMap.put("city",map.get("city"));
            orgMap.put("gps",map.get("gps"));
            orgMap.put("manager",map.get("manager"));
            orgMap.put("managerTel",map.get("managerPhone"));
            orgMap.put("account",managerPhone);
            orgMap.put("accountId",accountId);
            String orgId=orgSyncClient.syncCreateOrg(orgMap);
            map.put("syncOrgId",orgId);
        }catch (Exception exception){
            exception.printStackTrace();
        }
        super.add(map);

    }
    public void modify(){
        causes(BasicModifyEvent.class,this);
    }
    public void modify(Map map){
        map.remove("account");
        map.remove("accountId");
        map.remove("deviceMac");
        map.remove("devicePassword");
        super.modify(map);
    }
    public void delete(UserClient client){
        client.deleteUser(getAccountId());
        super.delete();
    }

    public void addDevice(String mac) {
        if(deviceMac==null)
            deviceMac=new HashMap();
        if(deviceMac.size()>=deviceCount)
            throw new DomainException("设备数量已达上限");
        deviceMac.put(mac,"enabled");
        causes(BasicModifyEvent.class,this);
    }
    public int newSerialNum(){
        int number= getMemberSerial()+1;
        setMemberSerial(number);
        causes(BasicModifyEvent.class,this);
        return number;
    }

    public void modifyCustomInfo(Map map) {
        setC_name(map.get("c_name").toString());
        setC_printFoot(map.get("c_printFoot").toString());
        causes(BasicModifyEvent.class,this);
    }
    public void handerTest(){
        if(getServiceType().equals("frequency")){
            if(getTestNumber()<=0){
                throw new DomainException("您的评测次数已经用完");
            }
            setTestNumber(getTestNumber()-1);
        }
        modify();
    }

    public void handlerDownloadCourse() {
        if(getServiceType().equals("frequency")){
            if(getCourseNumber()<=0){
                throw new DomainException("您的课程下载次数已经用完");
            }
            setCourseNumber(getCourseNumber()-1);
        }
        modify();
    }
}
